package com.ruoyi.msg.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.msg.domain.MsgDcwjWjTk;
import com.ruoyi.msg.domain.bo.MsgDcwjWjTkBo;
import com.ruoyi.msg.domain.vo.MsgDcwjWjTkVo;
import com.ruoyi.msg.mapper.MsgDcwjWjTkMapper;
import com.ruoyi.msg.service.IMsgDcwjWjTkService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 问卷选项Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-02
 */
@RequiredArgsConstructor
@Service
public class MsgDcwjWjTkServiceImpl implements IMsgDcwjWjTkService {

    private final MsgDcwjWjTkMapper baseMapper;

    /**
     * 查询问卷选项
     */
    @Override
    public MsgDcwjWjTkVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询问卷选项列表
     */
    @Override
    public TableDataInfo<MsgDcwjWjTkVo> queryPageList(MsgDcwjWjTkBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MsgDcwjWjTk> lqw = buildQueryWrapper(bo);
        Page<MsgDcwjWjTkVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询问卷选项列表
     */
    @Override
    public List<MsgDcwjWjTkVo> queryList(MsgDcwjWjTkBo bo) {
        LambdaQueryWrapper<MsgDcwjWjTk> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MsgDcwjWjTk> buildQueryWrapper(MsgDcwjWjTkBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MsgDcwjWjTk> lqw = Wrappers.lambdaQuery();
        return lqw;
    }

    /**
     * 新增问卷选项
     */
    @Override
    public Boolean insertByBo(MsgDcwjWjTkBo bo) {
        MsgDcwjWjTk add = BeanUtil.toBean(bo, MsgDcwjWjTk.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改问卷选项
     */
    @Override
    public Boolean updateByBo(MsgDcwjWjTkBo bo) {
        MsgDcwjWjTk update = BeanUtil.toBean(bo, MsgDcwjWjTk.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MsgDcwjWjTk entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除问卷选项
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
